const QuickTuiOptions = {
	
	hotkeyConfig: new SOHotkeyConfig("platformKeys", "localeKeys"),
	
	hotkeyIds: ["hotkeyContentDialog", "hotkeyEmptyDialog"],
		
	onLoad: function() {
		QuickTuiUtils.log("onload");
		
		var $ = QuickTuiUtils.$;
		
		// Retrieve username and password from Login Manager
		// TODO: support multi-account futhur
		var loginManager = new SOLoginManager();
		var account = loginManager.getCurrentAccounts();
		
		if (account) {
			$("twitter-username").value = account.user;
			$("twitter-password").value = account.pass;
		}
		
		// init short url service item
		var curShortService = QuickTuiUtils.prefs.getCharPref("shortUrlService");
		if (curShortService && curShortService === "custom") {
			$("url-short-custom").disabled = false;
		}
			
		// Init the hot key
		for (var i=0; i < this.hotkeyIds.length; i++) {
			var hotkeyTextbox = $("quicktui-" + this.hotkeyIds[i]);
			var hotkeyPref = QuickTuiUtils.prefs.getCharPref(this.hotkeyIds[i]);
			this.hotkeyConfig.init(hotkeyTextbox, hotkeyPref);
		}
	},
	
	
	onAccept: function() {
		QuickTuiUtils.log("onaccept");
		
		var $ = QuickTuiUtils.$;
		
		// save hotkey config
		for (var i=0; i < this.hotkeyIds.length; i++) {
			var hotkeyTextbox = $("quicktui-" + this.hotkeyIds[i]);
			QuickTuiUtils.prefs.setCharPref(this.hotkeyIds[i], hotkeyTextbox.pref);
		}
	
		// save custom short url
		if ($("url-short-services").selectedItem.value != "custom") {
			QuickTuiUtils.prefs.setCharPref("shortUrl", $("url-short-custom").value);
		}
		if ($("url-short-services").selectedItem.value == "custom" 
				&& QuickTuiUtils.trim($("url-short-custom").value) == "") {
			QuickTuiUtils.prefs.setCharPref("shortUrl", "");
		}
	},
	
	onUnload: function() {
		QuickTuiUtils.log("ononload");
	    if (navigator.platform.match("Mac")) {
	      this.onAccept();
	    }
	},
	
	onCancel: function() {
		QuickTuiUtils.log("oncancel");
		
	},
	
	saveAccount: function() {
		QuickTuiUtils.log("saveAccount");
		
		var $ = QuickTuiUtils.$;

		var user = $("twitter-username").value;
		var pass = $("twitter-password").value;
		
		// validate the twitter account
		var twitterHelper = new SOTwitterHelper();
		twitterHelper.setBasicAuth(user, pass);
		twitterHelper.setApi(QuickTuiUtils.prefs.getCharPref("api"));
		//var validated = twitterHelper.validateAccount();
		var validated = twitterHelper.validateXAuthToken();
		
		var notiBox = $("quicktui-options-notification");
		if (validated) {
			// save account to login manager
			var loginManager = new SOLoginManager();
			loginManager.savePassword(user, pass);
			
			QuickTuiUtils.msg.showInfoNotification(notiBox, "The account is validated and saved success");
		} else {
			QuickTuiUtils.msg.showErrorNotification(notiBox, "Validate failure, please check again!");
			QuickTuiUtils.msg.showErrorNotification(notiBox, twitterHelper.message);
		}
	},
	
	recognize: function(e) {
		this.hotkeyConfig.recognize(e);
	},
	
	revert: function(e) {
		this.hotkeyConfig.revert(e);
	},
	
	resetPrefix: function(e) {
		var target = e.target.previousSibling;
		target.value = "";
		QuickTuiUtils.prefs.setCharPref("prefix", target.value);
	},
	
	resetSuffix: function(e) {
		var target = e.target.previousSibling;
		target.value = "#qTui";
		QuickTuiUtils.prefs.setCharPref("suffix", target.value);
	},
	
	onSelectShortService: function() {
		var $ = QuickTuiUtils.$;
		
		var list = $("url-short-services");
		var customBox = $("url-short-custom");
		
		customBox.value = list.selectedItem.getAttribute("defaultValue");
		customBox.disabled = (list.selectedItem.value != "custom");
	}
	
}